package tribe.vilt.lost.activity;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import tribe.vilt.lost.R;
import tribe.vilt.lost.util.C2DMessaging;

/**
 * Created by Tobias Ericsson
 */
public class HomeActivity extends Activity implements View.OnClickListener {

    private static final String TAG = HomeActivity.class.getSimpleName();
    private static final String REGISTER = "Register";
    private static final String UNREGISTER = "Unregister";
    private static final String CONNECTING = "Connecting ... ";
    public static final String GOOGLE_ACCOUNT_TYPE = "com.google";
    private Account[] allGoogleAccounts;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String text;
        if (isRegistered()) {
            displayRegisteredGmailAccount();
            text = UNREGISTER;
        } else {
            displayGmailAccounts();
            text = REGISTER;
        }

        Button button = (Button) findViewById(R.id.registerButton);
        button.setClickable(true);
        button.setText(text);
        button.setOnClickListener(this);
    }

    private boolean isRegistered() {
        SharedPreferences sharedPreferences = getSharedPreferences(C2DMessaging.SHARED_PREFS, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("registered", false);
    }

    private void displayRegisteredGmailAccount() {
        SharedPreferences sharedPreferences = getSharedPreferences(C2DMessaging.SHARED_PREFS, Context.MODE_PRIVATE);
        RadioGroup list = (RadioGroup) findViewById(R.id.gmail_account_list);
        list.setVisibility(View.GONE);
        TextView view = (TextView) findViewById(R.id.status_text);
        String gmail = sharedPreferences.getString("gmail", "unknown");
        view.setText(gmail + " is registered.");
    }

    private void displayGmailAccounts() {


        AccountManager mAccountManager = AccountManager.get(this);
        allGoogleAccounts = mAccountManager.getAccountsByType(
                GOOGLE_ACCOUNT_TYPE);
        int k = 0;

        RadioGroup list = (RadioGroup) findViewById(R.id.gmail_account_list);
        list.setVisibility(View.VISIBLE);
        boolean checked = true;
        while (k < allGoogleAccounts.length && allGoogleAccounts[k].name.contains("@gmail")) {
            Log.d(TAG, "user account: " + allGoogleAccounts[k].name);

            View view = getLayoutInflater().inflate(R.layout.gmail_account_element, list);

            TextView name = (TextView) view.findViewById(R.id.gmail_name);
            name.setText(allGoogleAccounts[k].name);

            RadioButton radioButton = (RadioButton) view.findViewById(R.id.radio_button);
            radioButton.setOnClickListener(this);
            radioButton.setTag(k);
            radioButton.setChecked(checked);


            //list.addView(view);
            if (checked) {
                SharedPreferences.Editor editor = getSharedPreferences("lost", 0).edit();
                editor.putString("gmail", allGoogleAccounts[k].name);
                editor.commit();
                checked = false;
            }

            k++;
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        Log.d(TAG, "onNewIntent");
        String text;
        if (isRegistered()) {
            displayRegisteredGmailAccount();
            text = UNREGISTER;
        } else {
            displayErrorMessage();
            text = REGISTER;
        }


        Button button = (Button) findViewById(R.id.registerButton);
        button.setClickable(true);
        button.setText(text);
        button.setOnClickListener(this);
    }

    private void displayErrorMessage() {
        Toast.makeText(this, "Failed, try again!!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {

        Integer k = (Integer) view.getTag();
        Log.d(TAG, "clicked " + k);
        if (k == null) {
            Button button = ((Button) view);
            button.setClickable(false);
            if (REGISTER.equals(button.getText())) {
                C2DMessaging.register(HomeActivity.this, "tribe.vilt@gmail.com");
            } else if (UNREGISTER.equals(button.getText())) {
                C2DMessaging.unregister(this);
            }
            ((Button) view).setText(CONNECTING);

        } else {
            if (((RadioButton) view).isChecked()) {
                ((RadioButton) view).setChecked(false);
            } else {
                ((RadioButton) view).setChecked(true);
                SharedPreferences.Editor editor = getSharedPreferences("lost", 0).edit();
                editor.putString("gmail", allGoogleAccounts[k].name);
                editor.commit();
            }

        }


    }
}
